import 'package:finance_app/src/entities/payment_detail_entity.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/cpf_contribution_entity.dart';
import 'package:finance_app/src/entities/payslips_item_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/page/dashboard/expense/new_expense_page.dart';

paymentDetailEntityFromJson(PaymentDetailEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['flow_type'] != null) {
		data.flowType = json['flow_type'].toString();
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['number'] != null) {
		data.number = json['number'].toString();
	}
	if (json['contact_id'] != null) {
		data.contactId = json['contact_id'] is String
				? int.tryParse(json['contact_id'])
				: json['contact_id'].toInt();
	}
	if (json['account_id'] != null) {
		data.accountId = json['account_id'] is String
				? int.tryParse(json['account_id'])
				: json['account_id'].toInt();
	}
	if (json['payment_type_id'] != null) {
		data.paymentTypeId = json['payment_type_id'] is String
				? int.tryParse(json['payment_type_id'])
				: json['payment_type_id'].toInt();
	}
	if (json['tax_id'] != null) {
		data.taxId = json['tax_id'] is String
				? int.tryParse(json['tax_id'])
				: json['tax_id'].toInt();
	}
	if (json['is_tax_inclusive'] != null) {
		data.isTaxInclusive = json['is_tax_inclusive'] is String
				? int.tryParse(json['is_tax_inclusive'])
				: json['is_tax_inclusive'].toInt();
	}
	if (json['tax'] != null) {
		data.tax = SettingDataTax().fromJson(json['tax']);
	}
	if (json['issued_at'] != null) {
		data.issuedAt = json['issued_at'].toString();
	}
	if (json['numbers'] != null) {
		data.numbers = (json['numbers'] as List).map((v) => v.toString()).toList().cast<String>();
	}
	if (json['number_ids'] != null) {
		data.numberIds = (json['number_ids'] as List).map((v) => v is String
				? int.tryParse(v)
				: v.toInt()).toList().cast<int>();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['total'] != null) {
		data.total = json['total'] is String
				? num.tryParse(json['total'])
				: json['total'];
	}
	if (json['cheque_number'] != null) {
		data.chequeNumber = json['cheque_number'];
	}
	if (json['notes'] != null) {
		data.notes = json['notes'].toString();
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['status'] != null) {
		data.status = json['status'].toString();
	}
	if (json['expense'] != null) {
		data.expense = (json['expense'] as List).map((v) => ExpenseDetailEntity().fromJson(v)).toList();
	}
	if (json['cpfcontributions'] != null) {
		data.cpfcontributions = (json['cpfcontributions'] as List).map((v) => CpfContributionEntity().fromJson(v)).toList();
	}
	if (json['payslips'] != null) {
		data.payslips = (json['payslips'] as List).map((v) => PayslipsItemEntity().fromJson(v)).toList();
	}
	if (json['attachments'] != null) {
		data.attachments = (json['attachments'] as List).map((v) => OcrAttachmentEntity().fromJson(v)).toList();
	}
	if (json['pivot'] != null) {
		data.pivot = PaymentDetailPivot().fromJson(json['pivot']);
	}
	if (json['account'] != null) {
		data.account = AccountEntity().fromJson(json['account']);
	}
	if (json['contact'] != null) {
		data.contact = BusinessContactEntity().fromJson(json['contact']);
	}
	if (json['account_total'] != null) {
		data.accountTotal = json['account_total'] is String
				? num.tryParse(json['account_total'])
				: json['account_total'];
	}
	return data;
}

Map<String, dynamic> paymentDetailEntityToJson(PaymentDetailEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['flow_type'] = entity.flowType;
	data['type'] = entity.type;
	data['name'] = entity.name;
	data['number'] = entity.number;
	data['contact_id'] = entity.contactId;
	data['account_id'] = entity.accountId;
	data['payment_type_id'] = entity.paymentTypeId;
	data['tax_id'] = entity.taxId;
	data['is_tax_inclusive'] = entity.isTaxInclusive;
	data['tax'] = entity.tax?.toJson();
	data['issued_at'] = entity.issuedAt;
	data['numbers'] = entity.numbers;
	data['number_ids'] = entity.numberIds;
	data['currency_code'] = entity.currencyCode;
	data['total'] = entity.total;
	data['cheque_number'] = entity.chequeNumber;
	data['notes'] = entity.notes;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['status'] = entity.status;
	data['expense'] =  entity.expense?.map((v) => v.toJson())?.toList();
	data['cpfcontributions'] =  entity.cpfcontributions?.map((v) => v.toJson())?.toList();
	data['payslips'] =  entity.payslips?.map((v) => v.toJson())?.toList();
	data['attachments'] =  entity.attachments?.map((v) => v.toJson())?.toList();
	data['pivot'] = entity.pivot?.toJson();
	data['account'] = entity.account?.toJson();
	data['contact'] = entity.contact?.toJson();
	data['account_total'] = entity.accountTotal;
	return data;
}

paymentDetailPivotFromJson(PaymentDetailPivot data, Map<String, dynamic> json) {
	if (json['paymentable_id'] != null) {
		data.paymentabled = json['paymentable_id'] is String
				? int.tryParse(json['paymentable_id'])
				: json['paymentable_id'].toInt();
	}
	if (json['payment_id'] != null) {
		data.paymentId = json['payment_id'] is String
				? int.tryParse(json['payment_id'])
				: json['payment_id'].toInt();
	}
	if (json['amount'] != null) {
		data.amount = json['amount'].toString();
	}
	return data;
}

Map<String, dynamic> paymentDetailPivotToJson(PaymentDetailPivot entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['paymentable_id'] = entity.paymentabled;
	data['payment_id'] = entity.paymentId;
	data['amount'] = entity.amount;
	return data;
}